/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIRelationType;
import com.carnation.mong.platform.service.RelationTypeService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class RelationTypeServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private RelationTypeService service;

	public RelationTypeServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(RelationTypeService.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getAncestorRTs".equals(method)) {
			returnStr = getAncestorRTs(item);
		} else if ("getChildRTs".equals(method)) {
			returnStr = getChildRTs(item);
		} else if ("getDescendantRTs".equals(method)) {
			returnStr = getDescendantRTs(item);
		} else if ("getParentRT".equals(method)) {
			returnStr = getParentRT(item);
		} else if ("getPath".equals(method)) {
			returnStr = getPath(item);
		} else if ("getRT".equals(method)) {
			returnStr = getRT(item);
		} else if ("getRTByPath".equals(method)) {
			returnStr = getRTByPath(item);
		} else if ("isInheritedFrom".equals(method)) {
			returnStr = isInheritedFrom(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("create MOIRelationType:" + i);
			}
			try {
				Object obj = service.create(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("delete MOIRelationType:" + i);
			}
			try {
				service.delete(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getAncestorRTs(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getAncestorRTs MOIRelationType:" + i);
			}
			try {
				Object obj = service.getAncestorRTs(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getChildRTs(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getChildRTs MOIRelationType:" + i);
			}
			try {
				Object obj = service.getChildRTs(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getDescendantRTs(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getDescendantRTs MOIRelationType:" + i);
			}
			try {
				Object obj = service.getDescendantRTs(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getParentRT(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getParentRT MOIRelationType:" + i);
			}
			try {
				Object obj = service.getParentRT(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPath(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getPath MOIRelationType:" + i);
			}
			try {
				Object obj = service.getPath(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRT(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String i = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getRT id:" + i);
			}
			try {
				Object obj = service.getRT(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRTByPath(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String i = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getRTByPath path:" + i);
			}
			try {
				Object obj = service.getRTByPath(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String isInheritedFrom(Item item) {
		if (item.getChildren().size() == 2) {// 参数个数
			MOIRelationType childRT = instanceMOIRelationType(item
					.getChildren().get(0));
			MOIRelationType parentRT = instanceMOIRelationType(item
					.getChildren().get(1));
			if (log.isDebugEnabled()) {
				log.debug("isInheritedFrom childRT:" + childRT + ",parentRT:"
						+ parentRT);
			}
			try {
				Object obj = service.isInheritedFrom(childRT, parentRT);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("update MOIRelationType:" + i);
			}
			try {
				service.update(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

}
